using System;
using FireStarter.Base;
using FireStarterModeller.BO;
using FireStarterModeller.Test.BO.Fakes;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using NUnit.Framework;
using Rhino.Mocks;

namespace FireStarterModeller.Test.BO
{
    /// <summary>
    /// Provides a place to write custom tests for UIView objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for UIView.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    public partial class TestUIView
    {
        [TestFixtureSetUp]
        public void SetupTestFixture()
        {
            //------------------------------------------------------------
            // Use this list to ignore generated tests that are failing
            // due to a unique condition in your application.
            // Remember to reimplement the test here.
            //------------------------------------------------------------
            //_ignoreList.Add("TestMethodName", "Reason for ignoring it");
            base.SetupFixture();
        }

        [Ignore("Need to implement ToString")] //TODO Brett 24 Feb 2009: Ignored Test - Need to implement ToString
        [Test]
        public void Test_ToString()
        {
            //---------------Set up test pack-------------------

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------

            //---------------Test Result -----------------------
            Assert.Fail("Implement ToString() for the Business object and implement this test");
        }

        [Test]
        public void Test_IUIDef_Name_Get()
        {
            //---------------Set up test pack-------------------
            UIView uiView = TestUtilsUIView.CreateUnsavedValidUIView();
            IUIDef uiDef = uiView;

            //---------------Assert Precondition----------------
            Assert.Greater(uiView.ViewName.Length, 0);
            //---------------Execute Test ----------------------
            string name = uiDef.Name;
            //---------------Test Result -----------------------
            Assert.AreEqual(uiView.ViewName, name);
        }

        [Test]
        public void Test_IUIDef_Name_Set()
        {
            //---------------Set up test pack-------------------
            UIView uiView = TestUtilsUIView.CreateUnsavedValidUIView();
            IUIDef uiDef = uiView;
            string name = TestUtilsShared.GetRandomString();

            //---------------Assert Precondition----------------
            Assert.IsNotNull(uiView.ViewName, name);
            //---------------Execute Test ----------------------
            uiDef.Name = name;
            //---------------Test Result -----------------------
            Assert.AreEqual(name, uiView.ViewName);
        }

        [Test]
        public void Test_IUIDef_UIForm_Get()
        {
            //---------------Set up test pack-------------------
            UIFormInfo formInfo = TestUtilsShared.CreateUnsavedValidUIFormInfo();
            UIView uiView = formInfo.UIView;
            IUIDef uiDef = uiView;

            //---------------Assert Precondition----------------
            Assert.IsNotNull(uiView);
            //---------------Execute Test ----------------------
            IUIForm uiForm = uiDef.UIForm;
            //---------------Test Result -----------------------
            Assert.AreSame(formInfo, uiForm);
        }


        [Test]
        public void Test_IUIDef_UIForm_Set()
        {
            //---------------Set up test pack-------------------
            UIView uiView = new UIView();
            IUIDef uiDef = uiView;
            UIFormInfo formInfo = new UIFormInfo();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            uiDef.UIForm = formInfo;
                //---------------Test Result -----------------------
            Assert.AreSame(formInfo, uiDef.UIForm);
        }

        [Test]
        public void Test_IUIDef_UIForm()
        {
            //---------------Set up test pack-------------------
            UIFormInfo formInfo = TestUtilsShared.CreateUnsavedValidUIFormInfo();
            UIView uiView = formInfo.UIView;
            IUIDef uiDef = uiView;

            //---------------Assert Precondition----------------
            Assert.IsNotNull(uiView);
            //---------------Execute Test ----------------------
            IUIForm uiForm = uiDef.UIForm;
            //---------------Test Result -----------------------
            Assert.AreSame(formInfo, uiForm);
        }

        [Test]
        public void Test_IUIDef_Get()
        {
            //---------------Set up test pack-------------------
            UIGridInfo gridInfo = TestUtilsShared.CreateUnsavedValidUIGridInfo();
            UIView uiView = gridInfo.UIView;
            IUIDef uiDef = uiView;

            //---------------Assert Precondition----------------
            Assert.IsNotNull(uiView);
            //---------------Execute Test ----------------------
            IUIGrid uiGrid = uiDef.UIGrid;
            //---------------Test Result -----------------------
            Assert.AreSame(gridInfo, uiGrid);
        }

        [Test]
        public void Test_IUIDef_UIGrid_Set()
        {
            //---------------Set up test pack-------------------
            UIView uiView = new UIView();
            IUIDef uiDef = uiView;
            UIGridInfo gridInfo = new UIGridInfo();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            uiDef.UIGrid = gridInfo;
            //---------------Test Result -----------------------
            Assert.AreSame(gridInfo, uiDef.UIGrid);
        }

        [Test]
        public void Test_IUIDef_UIGrid()
        {
            //---------------Set up test pack-------------------
            UIGridInfo gridInfo = TestUtilsShared.CreateUnsavedValidUIGridInfo();
            UIView uiView = gridInfo.UIView;
            IUIDef uiDef = uiView;

            //---------------Assert Precondition----------------
            Assert.IsNotNull(uiView);
            //---------------Execute Test ----------------------
            IUIGrid uiGrid = uiDef.UIGrid;
            //---------------Test Result -----------------------
            Assert.AreSame(gridInfo, uiGrid);
        }

        [Test]
        public void Test_ClassName_WhenClassNull_ShouldRetEmptyString()
        {
            //---------------Set up test pack-------------------
            UIView uiView = new UIView();
            //---------------Assert Precondition----------------
            Assert.IsNull(uiView.Class);
            //---------------Execute Test ----------------------
            string className = uiView.ClassName;
            //---------------Test Result -----------------------
            Assert.IsEmpty(className);
        }
        [Test]
        public void Test_ClassName_ShouldRetClass_ClassName()
        {
            //---------------Set up test pack-------------------
            UIView uiView = new UIViewFake();
            var dmClass = MockRepository.GenerateStub<DMClass>(ConstructForFakes.True);
            dmClass.ClassNameBO = GetRandomString();
            uiView.Class = dmClass;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(uiView.ClassDef);
            Assert.IsNotNullOrEmpty(dmClass.ClassNameBO);
            //---------------Execute Test ----------------------
            string className = uiView.ClassName;
            //---------------Test Result -----------------------
            Assert.AreSame(dmClass.ClassNameBO, className);
        }

        private static string GetRandomString()
        {
            return TestUtilsShared.GetRandomString();
        }

/*
        [Test]
        public void Test_ClassName_WhenClassDefNull_ShouldReturnEmptyString()
        {
            //---------------Set up test pack-------------------
            UIDefCol uiDefCol = new UIDefCol();
            //---------------Assert Precondition----------------
            Assert.IsNull(uiDefCol.ClassDef);
            //---------------Execute Test ----------------------
            string className = uiDefCol.ClassName;
            //---------------Test Result -----------------------
            Assert.IsEmpty(className);
        }
        [Test]
        public void Test_ClassName_WhenClassDefSet_ShouldReturnClassDefClassName()
        {
            //---------------Set up test pack-------------------
            UIDefCol uiDefCol = new UIDefCol();
            var classDef = MockRepository.GenerateStub<IClassDef>();
            classDef.ClassName = GetRandomString();
            uiDefCol.ClassDef = classDef;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(uiDefCol.ClassDef);
            Assert.IsNotNullOrEmpty(classDef.ClassName);
            //---------------Execute Test ----------------------
            string className = uiDefCol.ClassName;
            //---------------Test Result -----------------------
            Assert.AreSame(classDef.ClassName, className);
        }*/
        [Test]
        public void Test_GetParent()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = new DMClass();
            UIView view = new UIView();
            dmClass.UIViews.Add(view);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IModelElement parent = ((IModelElement)view).Parent;
            //---------------Test Result -----------------------
            Assert.AreSame(dmClass, parent);
        }

        [Test]
        public void Test_SetParentExceptionThrown()
        {
            //---------------Set up test pack-------------------
            IModelElement prop = new UIView();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            Exception exceptionThrown = null;
            try
            {
                prop.Parent = new DMClass();
            }
            catch (Exception ex)
            {
                exceptionThrown = ex;
            }
            //---------------Test Result -----------------------
            Assert.IsNotNull(exceptionThrown);
            Assert.IsInstanceOf(typeof(HabaneroDeveloperException), exceptionThrown);
            StringAssert.Contains
                ("Set the parent of the UIView by setting the related Class object", exceptionThrown.Message);
        }
    }

}